package com.net.framework.help.utils;

import android.content.Context;
import android.util.Log;

import java.lang.reflect.Field;

/**
 * @author zhangxiaobin
 * @email zhangxiaobin@yipintech.com
 * @date 创建时间：2015年6月30日 上午11:15:00
 * @version 1.1.0
 * @explain 取出资源id
 */
public class GetResources {

	public static int getItemId(Context paramContext, String paramString1, String paramString2) {
		try {
			Class<?> localClass = Class.forName(paramContext.getPackageName() + ".R$" + paramString1);
			Field localField = localClass.getField(paramString2);
			int i = Integer.parseInt(localField.get(localField.getName()).toString());
			return i;
		} catch (Exception localException) {
			Log.e("getIdByReflection error", localException.getMessage());
		}
		return 0;
	}

	public static int[] getItemIdArray(Context paramContext, String paramString1, String paramString2) {
		try {
			Class<?> localClass = Class.forName(paramContext.getPackageName() + ".R$" + paramString1);
			Field localField = localClass.getField(paramString2);
			int[] i = (int[]) localField.get(localField.getName().toString());
			return i;
		} catch (Exception localException) {
			Log.e("getIdByReflection error", localException.getMessage());
		}
		return null;
	}

	/***
	 * 取出布局文件（layout）资源id
	 * 
	 * @param context
	 * @param name
	 * @return
	 */
	public static int getLayoutResIDByName(Context context, String name) {
		return context.getResources().getIdentifier(name, "layout", context.getPackageName());
	}

	/***
	 * 取出控件id资源id
	 * 
	 * @param context
	 * @param name
	 * @return
	 */
	public static int getIdResIDByName(Context context, String name) {
		return context.getResources().getIdentifier(name, "id", context.getPackageName());
	}

	/***
	 * 取出string资源id
	 * 
	 * @param context
	 * @param name
	 * @return
	 */
	public static int getStringResIDByName(Context context, String name) {
		return context.getResources().getIdentifier(name, "string", context.getPackageName());
	}

	/****
	 * 取出动画（anim）资源id
	 * 
	 * @param context
	 * @param name
	 * @return
	 */
	public static int getAnimResIDByName(Context context, String name) {
		return context.getResources().getIdentifier(name, "anim", context.getPackageName());
	}

	/***
	 * 取出图片文件（Drawable）资源id
	 * 
	 * @param context
	 * @param name
	 * @return
	 */
	public static int getDrawableResIDByName(Context context, String name) {
		return context.getResources().getIdentifier(name, "drawable", context.getPackageName());
	}

	/**
	 * 取出（Dimen）资源id
	 */
	public static int getDimenResIDByName(Context context, String name) {
		return context.getResources().getIdentifier(name, "dimen", context.getPackageName());
	}

	/**
	 * 取出（Attr）属性
	 */
	public static int getAttrResIDByName(Context context, String name) {
		return context.getResources().getIdentifier(name, "attr", context.getPackageName());
	}

	/**
	 * 
	 * context.getResources().getIdentifier无法获取到styleable的数据
	 * 
	 * @paramcontext
	 * 
	 * @param name
	 * 
	 * @return
	 */

	public static int getStyleable(Context context, String name) {
		return ((Integer) getResourceId(context, name, "styleable")).intValue();
	}

	/**
	 * 
	 * 获取styleable的ID号数组
	 * 
	 * @paramcontext
	 * 
	 * @param name
	 * 
	 * @return
	 */

	public static int[] getStyleableArray(Context context, String name) {
		return (int[]) getResourceId(context, name, "styleable");
	}

	/**
	 * 
	 * 对于context.getResources().getIdentifier无法获取的数据,或者数组
	 * 
	 * 资源反射值
	 * 
	 * @paramcontext
	 * 
	 * @param name
	 * 
	 * @param type
	 * 
	 * @return
	 */

	private static Object getResourceId(Context context, String name, String type) {
		String className = context.getPackageName() + ".R";
		try {
			Class cls = Class.forName(className);
			for (Class childClass : cls.getClasses()) {
				String simple = childClass.getSimpleName();
				if (simple.equals(type)) {
					for (Field field : childClass.getFields()) {
						String fieldName = field.getName();
						if (fieldName.equals(name)) {
							return field.get(null);
						}
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
